package com.ss.android.video;

import android.content.Context;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.app.BizApplication;
import com.ss.android.article.base.feature.video.VideoPref;
import com.ss.android.newmedia.BaseApplication;

import org.json.JSONArray;

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import tv.danmaku.ijk.media.player.AndroidMediaPlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnBufferingUpdateListener;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnCompletionListener;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnErrorListener;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnInfoListener;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnPreparedListener;
import tv.danmaku.ijk.media.player.IMediaPlayer.OnSeekCompleteListener;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class SSMediaPlayerWrapper implements
        Callback,
        OnBufferingUpdateListener,
        OnCompletionListener,
        OnErrorListener,
        OnInfoListener,
        OnPreparedListener,
        OnSeekCompleteListener {
    public static final int CALLBACK_ON_BUFFERING_UPDATE = 301;
    public static final int CALLBACK_ON_COMPLETE = 302;
    public static final int CALLBACK_ON_ERROR = 303;
    public static final int CALLBACK_ON_INFO = 304;
    public static final int CALLBACK_ON_MP_EXCEPTION = 310;
    public static final int CALLBACK_ON_PREPARE = 305;
    public static final int CALLBACK_ON_RELEASED = 309;
    public static final int CALLBACK_ON_SEEK_COMPLETE = 306;
    public static final int CALLBACK_ON_START = 307;
    public static final int CALLBACK_ON_STATE_ERROR = 308;
    public static final int IJK_CLOSE_AUDIO_STREAM = 20201;
    public static final int IJK_OPEN_AUDIO_STREAM = 20202;
    private static final int OP_GET_READ_BYTES = 201;
    private static final int OP_PAUSE = 101;
    private static final int OP_PREPARE_ASYNC = 104;
    private static final int OP_RELEASE = 103;
    public static final int OP_REQUEST_CUR_POSITION = 109;
    public static final int OP_REQUEST_DURATION = 108;
    private static final int OP_RESET = 102;
    private static final int OP_SEEKTO = 106;
    private static final int OP_SET_DATASOURCE = 107;
    private static final int OP_SET_DISPLAY = 110;
    private static final int OP_SET_SURFACE = 111;
    private static final int OP_START = 100;
    private static final int OP_STOP = 105;
    private static final int STAT_END = 203;
    private static final int STAT_ERROR = 200;
    private static final int STAT_IDLE = 201;
    public static final int STAT_INITIALIZED = 202;
    public static final int STAT_PAUSED = 207;
    public static final int STAT_PLAYBACK_COMPLETE = 209;
    public static final int STAT_PREPARED = 205;
    public static final int STAT_STARTED = 206;
    public static final int STAT_STOPPED = 208;
    private static final String TAG = "SSMediaPlayeWrapper";
    public static final int TYPE_PLAYER_ANDROID = 0;
    public static final int TYPE_PLAYER_IJK = 1;
    boolean hasPendingPauseCommand;
    private Handler mCallbackHandler;
    int mCurrentState = STAT_IDLE;
    private boolean mExecutingActions;
    private long mFirstSeekToPosition = -1;
    private static int mHttpDNSIpError = 0;
    private boolean mIsReleasing;
    private static boolean mIsVolumeChanged = false;
    private long mLastReadBytes = 0;
    private int mLastVolume;
    public static final List mLogList = new ArrayList();
    private static Object mLogLock = new Object();
    private boolean mLogStateError = false;
    private boolean mLooping = false;
    IMediaPlayer mMediaPlayer = null;
    Handler mOpHandler;
    private ArrayList<Runnable> mPendingActions;
    public static int mediaPlayerType = 0;
    public static int newPlayerFailTime = 0;
    private StringBuilder readBytesBuilder = null;
    private Object readBytesLock = new Object();

    public SSMediaPlayerWrapper(Handler handler) {
        this(handler, false);
    }

    public SSMediaPlayerWrapper(Handler handler, boolean arg7) {
        SSMediaPlayerWrapper.mediaPlayerType = 0;
        if (!AppData.getInstance().an() && SSMediaPlayerWrapper.newPlayerFailTime < 3 && !arg7) {
            IjkMediaPlayer.loadLibrariesOnce(BaseApplication.getApplication());
            if (!IjkMediaPlayer.isLibLoaded()) {
            } else {
                SSMediaPlayerWrapper.mediaPlayerType = 1;
                try {
                    IjkMediaPlayer.setIPV6First(AppData.getInstance().videoPlayUseIpV6());
                    if (AppData.getInstance().getAbSettings().isEnableFeedBackWithVideoLog()) {
                        IjkMediaPlayer.setLogLevel(6);
                        if (AppData.getInstance().getAbSettings().getTcpReadTimeOut() > 1000000) {
                            IjkMediaPlayer.setTcpReadTimeOut(AppData.getInstance().getAbSettings().getTcpReadTimeOut());
                        } else {
                            IjkMediaPlayer.setTcpReadTimeOut(0);
                        }
                        IjkMediaPlayer.setIsUploadLog(1);
                    } else {
                        IjkMediaPlayer.setLogLevel(8);
                        IjkMediaPlayer.setTcpReadTimeOut(0);
                        IjkMediaPlayer.setIsUploadLog(0);
                    }
                } catch (Throwable throwable) {
                }
            }
        }
        this.mCallbackHandler = handler;
        HandlerThread handlerThread = new HandlerThread("VideoManager");
        handlerThread.start();
        this.mOpHandler = new WeakHandler(handlerThread.getLooper(), this);
        this.initMediaPlayer();
    }

    private void callBackOp(int op, Object obj) {
        if (op == CALLBACK_ON_RELEASED) {
            this.resetVolume();
        }

        if (this.mCallbackHandler != null) {
            this.mCallbackHandler.obtainMessage(op, obj).sendToTarget();
        }
    }

    private void clearPendingActions() {
        if (this.mPendingActions != null && !this.mPendingActions.isEmpty()) {
            this.mPendingActions.clear();
        }
    }

    private void doGetTcpReadBytes(boolean reset) {
        if ((AppData.getInstance().getAbSettings().isEnableFeedBackWithVideoLog()) && ((this.mMediaPlayer instanceof IjkMediaPlayer))) {
            synchronized (readBytesLock) {
                if (reset) {
                    this.readBytesBuilder = new StringBuilder();
                    this.mLastReadBytes = 0;
                } else {
                    if (this.readBytesBuilder != null) {
                        long tcpReadBytes = ((IjkMediaPlayer) this.mMediaPlayer).getTcpReadBytes();
                        this.readBytesBuilder.append(tcpReadBytes - this.mLastReadBytes + "_");
                        this.mLastReadBytes = tcpReadBytes;
                    }
                }
            }
            this.mOpHandler.removeMessages(OP_GET_READ_BYTES);
            this.mOpHandler.sendEmptyMessageDelayed(OP_GET_READ_BYTES, 10000);
        }
    }

    private void enqueueAction(Runnable runnable) {
        if (this.mPendingActions == null) {
            this.mPendingActions = new ArrayList();
        }
        this.mPendingActions.add(runnable);
    }

    public void execAction(Runnable runnable) {
        if (runnable != null) {
            if (!this.mIsReleasing) {
                runnable.run();
            } else {
                this.enqueueAction(runnable);
            }
        }
    }

    public void execPendingActions() {
        if (!this.mExecutingActions && this.mPendingActions != null && !this.mPendingActions.isEmpty()) {
            this.mExecutingActions = true;
            Iterator<Runnable> iterator = new ArrayList(this.mPendingActions).iterator();
            while (iterator.hasNext()) {
                iterator.next().run();
            }
            this.mPendingActions.clear();
            this.mExecutingActions = false;
        }
    }

    public int getCurrentState() {
        return this.mCurrentState;
    }

    public int getCurrentVolume() {
        Context context = BaseApplication.getApplication();
        return ((AudioManager) context.getSystemService(Context.AUDIO_SERVICE)).getStreamVolume(AudioManager.STREAM_MUSIC);
    }

    public static JSONArray getLogList() {
        JSONArray jsonArray;
        if (SSMediaPlayerWrapper.mLogList.size() > 0) {
            synchronized (mLogLock) {
                jsonArray = new JSONArray(SSMediaPlayerWrapper.mLogList);
                SSMediaPlayerWrapper.mLogList.clear();
            }
        } else {
            jsonArray = null;
        }
        return jsonArray;
    }

    public int getMaxVolume() {
        return ((AudioManager) BaseApplication.getApplication().getSystemService(Context.AUDIO_SERVICE)).getStreamMaxVolume(3);
    }

    public void handleMsg(Message message) {
        int what = message.what;
        if (this.mMediaPlayer == null && what == OP_RELEASE) {
            this.callBackOp(CALLBACK_ON_RELEASED, null);
        }

        Integer exCode = null;
        if (this.mMediaPlayer != null) {
            boolean error = false;
            switch (message.what) {
                case OP_START: {
                    if (this.mCurrentState != STAT_PREPARED
                            && this.mCurrentState != STAT_STARTED
                            && this.mCurrentState != STAT_PAUSED
                            && this.mCurrentState != STAT_PLAYBACK_COMPLETE) {
                        error = true;
                    } else {
                        try {
                            this.mMediaPlayer.start();
                            this.mCurrentState = STAT_STARTED;
                            if (this.mFirstSeekToPosition > 0) {
                                this.mMediaPlayer.seekTo(this.mFirstSeekToPosition);
                                this.mFirstSeekToPosition = -1;
                            }
                            this.doGetTcpReadBytes(true);
                        } catch (Exception exception) {
                            exCode = Integer.valueOf(1004);
                            VideoPref.removeLastVideoIdAndPos();
                        }
                    }
                    break;
                }
                case OP_PAUSE: {
                    if (this.mCurrentState != STAT_STARTED
                            && this.mCurrentState != STAT_PAUSED
                            && this.mCurrentState != STAT_PLAYBACK_COMPLETE) {
                        error = true;
                    } else {
                        try {
                            this.mMediaPlayer.pause();
                            this.mCurrentState = STAT_PAUSED;
                            this.hasPendingPauseCommand = false;
                        } catch (Exception e) {
                            exCode = Integer.valueOf(1005);
                        }
                    }
                    break;
                }
                case OP_RESET: {
                    try {
                        this.mMediaPlayer.reset();
                        this.mCurrentState = STAT_IDLE;
                    } catch (Exception exception) {
                        exCode = Integer.valueOf(1006);
                    }
                    break;
                }
                case OP_RELEASE: {
                    try {
                        this.mMediaPlayer.release();
                    } catch (Exception e) {
                        e.printStackTrace();
                        exCode = Integer.valueOf(1009);
                    }
                    this.mIsReleasing = false;
                    this.callBackOp(CALLBACK_ON_RELEASED, null);
                    this.mCurrentState = STAT_END;
                    this.mMediaPlayer = null;
                    break;
                }
                case OP_PREPARE_ASYNC: {
                    if (this.mCurrentState != STAT_INITIALIZED && this.mCurrentState != STAT_STOPPED) {
                        error = true;
                    } else {
                        try {
                            if (SSMediaPlayerWrapper.mediaPlayerType == 0) {
                                ((AndroidMediaPlayer) this.mMediaPlayer).getInternalMediaPlayer().prepare();
                                this.mCurrentState = STAT_PREPARED;
                            } else {
                                this.mMediaPlayer.prepareAsync();
                            }
                        } catch (Exception exception) {
                            exception.printStackTrace();
                            exCode = Integer.valueOf(1003);
                        }
                    }
                    break;
                }
                case OP_STOP: {
                    if (this.mCurrentState != STAT_PREPARED
                            && this.mCurrentState != STAT_STARTED
                            && this.mCurrentState != STAT_STOPPED
                            && this.mCurrentState != STAT_PAUSED
                            && this.mCurrentState != STAT_PLAYBACK_COMPLETE) {
                        error = true;
                    } else {
                        try {
                            this.mMediaPlayer.stop();
                            this.mCurrentState = STAT_STOPPED;
                        } catch (Exception e) {
                            exCode = Integer.valueOf(1008);
                        }
                    }
                    break;
                }
                case OP_SEEKTO: {
                    if (this.mCurrentState != STAT_STARTED
                            && this.mCurrentState != STAT_PAUSED
                            && this.mCurrentState != STAT_PLAYBACK_COMPLETE) {
                        error = true;
                    } else {
                        try {
                            this.mMediaPlayer.seekTo((Long) message.obj);
                        } catch (Exception exception) {
                            exCode = Integer.valueOf(1007);
                        }
                    }
                    break;
                }
                case OP_SET_DATASOURCE: {
                    if (this.mCurrentState != STAT_IDLE && this.mCurrentState != STAT_END) {
                        error = true;
                    } else {
                        try {
                            String url = (String) message.obj;
                            if (TTUtils.isValidUrl(url)) {
                                int v1_4;
                                if (!AppData.getInstance().getAbSettings().isVideoUseIpUrl()) {
                                    v1_4 = 0;
                                } else if (SSMediaPlayerWrapper.mHttpDNSIpError < 3) {
                                    Uri uri = Uri.parse(url);
                                    if (uri == null) {
                                        v1_4 = 0;
                                    } else if ("127.0.0.1".equals(uri.getHost())) {
                                        v1_4 = 0;
                                    } else {
                                        List<InetAddress> inetAddresses = this.resolveInetAddresses(uri.getHost());
                                        if (inetAddresses == null) {
                                            v1_4 = 0;
                                        } else if (inetAddresses.size() != 0) {
                                            InetAddress inetAddress = inetAddresses.get(new Random().nextInt(inetAddresses.size()));
                                            if (inetAddress != null) {
                                                String hostAddress = inetAddress.getHostAddress();
                                                if (!TextUtils.isEmpty(hostAddress)) {
                                                    Uri uri1 = Uri.parse(url.replace(uri.getHost(), hostAddress));
                                                    if (uri1 != null) {
                                                        HashMap<String, String> hashMap = new HashMap();
                                                        hashMap.put("Host", " " + uri.getHost());
                                                        this.mMediaPlayer.setDataSource(BizApplication.getApplication(), uri1, ((Map) hashMap));
                                                        v1_4 = 1;
                                                    } else {
                                                        v1_4 = 0;
                                                    }
                                                } else {
                                                    v1_4 = 0;
                                                }
                                            } else {
                                                v1_4 = 0;
                                            }
                                        } else {
                                            v1_4 = 0;
                                        }
                                    }
                                } else {
                                    v1_4 = 0;
                                }
                                if (v1_4 == 0) {
                                    this.mMediaPlayer.setDataSource(url);
                                }
                            } else {
                                this.mMediaPlayer.setDataSource(BizApplication.getApplication(), Uri.parse(((String) url)));
                            }
                            this.mCurrentState = STAT_INITIALIZED;
                        } catch (Exception exception) {
                            exception.printStackTrace();
                            exCode = Integer.valueOf(1001);
                        }
                    }
                    break;
                }
                case OP_REQUEST_DURATION: {
                    long duration = 0;
                    if (mCurrentState == STAT_PAUSED || mCurrentState == STAT_STARTED) {
                        try {
                            duration = mMediaPlayer.getDuration();
                        } catch (Exception exception) {
                            exCode = Integer.valueOf(1010);
                        }
                    }
                    this.callBackOp(OP_REQUEST_DURATION, Long.valueOf(duration));
                    break;
                }
                case OP_REQUEST_CUR_POSITION: {
                    long currentPosition = 0;
                    if (this.mCurrentState == STAT_STARTED || this.mCurrentState == STAT_PAUSED) {
                        try {
                            currentPosition = this.mMediaPlayer.getCurrentPosition();
                        } catch (Exception exception) {
                            exCode = Integer.valueOf(1011);
                        }
                    }
                    this.callBackOp(OP_REQUEST_CUR_POSITION, Long.valueOf(currentPosition));
                    break;
                }
                case OP_SET_DISPLAY: {
                    try {
                        this.mMediaPlayer.setDisplay((SurfaceHolder) message.obj);
                        this.mMediaPlayer.setScreenOnWhilePlaying(true);
                    } catch (Exception exception) {
                        exCode = Integer.valueOf(1002);
                        Logger.e("SSMediaPlayeWrapper", exception.getMessage());
                    }
                    break;
                }
                case OP_SET_SURFACE: {
                    try {
                        this.mMediaPlayer.setSurface((Surface) message.obj);
                        this.mMediaPlayer.setWakeMode(BaseApplication.getApplication(), 10);
                    } catch (Exception exception) {
                        exCode = Integer.valueOf(1002);
                        Logger.e("SSMediaPlayeWrapper", exception.getMessage());
                    }
                    break;
                }
                case OP_GET_READ_BYTES: {
                    this.doGetTcpReadBytes(false);
                    break;
                }
                default:
                    break;
            }

            if (exCode != null) {
                Logger.e("SSMediaPlayeWrapper", "MediaPlayer exception: " + exCode.intValue() + " at state: " + what);
                this.callBackOp(CALLBACK_ON_MP_EXCEPTION, exCode);
            }

            if (error) {
                Logger.e("SSMediaPlayeWrapper", "Cause error at state " + what + " currentState: " + this.mCurrentState);
                this.mCurrentState = STAT_ERROR;
                if (!this.mLogStateError) {
                    this.callBackOp(CALLBACK_ON_STATE_ERROR, Integer.valueOf(what));
                    this.mLogStateError = true;
                }
            }
        }
    }

    public static void increaseNewPlayerFailTime() {
        if (SSMediaPlayerWrapper.mediaPlayerType == 1) {
            ++SSMediaPlayerWrapper.newPlayerFailTime;
        }

        ++SSMediaPlayerWrapper.mHttpDNSIpError;
    }

    void initMediaPlayer() {
        if (this.mMediaPlayer == null) {
            if (SSMediaPlayerWrapper.mediaPlayerType == 1) {
                Logger.d("SSMediaPlayeWrapper", "SSMediaPlayerWrapper use IJKMediaPlayer");
                this.mMediaPlayer = new IjkMediaPlayer(BaseApplication.getApplication());
            } else {
                Logger.d("SSMediaPlayeWrapper", "SSMediaPlayerWrapper use System Mediaplayer");
                this.mMediaPlayer = new AndroidMediaPlayer();
            }

            this.mMediaPlayer.setOnPreparedListener(((OnPreparedListener) this));
            this.mMediaPlayer.setOnCompletionListener(((OnCompletionListener) this));
            this.mMediaPlayer.setOnErrorListener(((OnErrorListener) this));
            this.mMediaPlayer.setOnBufferingUpdateListener(((OnBufferingUpdateListener) this));
            this.mMediaPlayer.setOnSeekCompleteListener(((OnSeekCompleteListener) this));
            this.mMediaPlayer.setOnInfoListener(((OnInfoListener) this));
            this.mMediaPlayer.setLooping(this.mLooping);
            this.mLogStateError = false;
        }
    }

    public boolean isPaused() {
        if (this.mCurrentState != STAT_PAUSED && !this.hasPendingPauseCommand) {
            return false;
        } else if (!this.mOpHandler.hasMessages(OP_START)) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isPlaying() {
        if (this.mCurrentState != STAT_STARTED && !this.mOpHandler.hasMessages(OP_START)) {
            return false;
        } else if (!this.hasPendingPauseCommand) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isPrepared() {
        return this.mCurrentState == STAT_PREPARED;
    }

    public boolean isStarted() {
        return this.isPrepared() || this.isPlaying() || this.isPaused();
    }

    public void onBufferingUpdate(IMediaPlayer arg4, int arg5) {
        if (this.mMediaPlayer == arg4 && this.mCallbackHandler != null) {
            this.mCallbackHandler.obtainMessage(CALLBACK_ON_BUFFERING_UPDATE, Integer.valueOf(arg5)).sendToTarget();
        }
    }

    public void onCompletion(IMediaPlayer mediaPlayer) {
        int currentState = !this.mLooping ? STAT_PLAYBACK_COMPLETE : STAT_STARTED;
        this.mCurrentState = currentState;
        if (SSMediaPlayerWrapper.mediaPlayerType == 1) {
            SSMediaPlayerWrapper.newPlayerFailTime = 0;
        }

        SSMediaPlayerWrapper.mHttpDNSIpError = 0;
        if (this.mCallbackHandler != null) {
            this.mCallbackHandler.obtainMessage(CALLBACK_ON_COMPLETE).sendToTarget();
        }

        this.removeTcpReadBytesMsg("completion");
    }

    public void onDestroy() {
        if (this.mOpHandler != null && this.mOpHandler.getLooper() != null) {
            try {
                this.mOpHandler.getLooper().quit();
            } catch (Exception ex) {
            }
        }
    }

    public boolean onError(IMediaPlayer mediaPlayer, int arg4, int arg5) {
        this.mCurrentState = STAT_ERROR;
        SSMediaPlayerWrapper.increaseNewPlayerFailTime();
        if (this.mCallbackHandler != null) {
            this.mCallbackHandler.obtainMessage(CALLBACK_ON_ERROR, arg4, arg5).sendToTarget();
        }

        return false;
    }

    public static void onIjkLog(String arg3) {
        Logger.e("SSMediaPlayerWrapper", "onIjkLog : " + arg3);
        if (!TextUtils.isEmpty(((CharSequence) arg3))) {
            synchronized (mLogLock) {
                if (SSMediaPlayerWrapper.mLogList.size() > 100) {
                    IjkMediaPlayer.setIsUploadLog(0);
                }
                if (SSMediaPlayerWrapper.mLogList != null) {
                    SSMediaPlayerWrapper.mLogList.add(arg3);
                }
            }
        }
    }

    public boolean onInfo(IMediaPlayer arg4, int arg5, int arg6) {
        if (this.mMediaPlayer == arg4 && this.mCallbackHandler != null) {
            this.mCallbackHandler.obtainMessage(CALLBACK_ON_INFO, arg5, arg6).sendToTarget();
        }
        return false;
    }

    public void onPrepared(IMediaPlayer mediaPlayer) {
        this.mCurrentState = STAT_PREPARED;
        if (SSMediaPlayerWrapper.mediaPlayerType == 1) {
            SSMediaPlayerWrapper.newPlayerFailTime = 0;
        }

        if (this.hasPendingPauseCommand) {
            this.mOpHandler.post(new Runnable() {
                public final void run() {
                    try {
                        mMediaPlayer.pause();
                        mCurrentState = STAT_PAUSED;
                        hasPendingPauseCommand = false;
                    } catch (Exception exception) {
                    }
                }
            });
        } else {
            this.mOpHandler.sendMessage(this.mOpHandler.obtainMessage(OP_START, -1, -1));
        }

        if (this.mCallbackHandler != null) {
            this.mCallbackHandler.sendEmptyMessage(CALLBACK_ON_PREPARE);
        }
    }

    public void onRelease() {
        int v0 = 1;
        int v1 = AppData.getInstance().getAbSettings().getVideoFixONLineQuestionFlage();
        if (v1 != 1 && (v1 & 4) <= 0) {
            v0 = 0;
        }

        if (v0 != 0 && !this.mIsReleasing && this.mCurrentState != STAT_END) {
            this.release();
        }
    }

    public void onSeekComplete(IMediaPlayer mediaPlayer) {
        if (this.mCallbackHandler != null) {
            this.mCallbackHandler.sendEmptyMessage(CALLBACK_ON_SEEK_COMPLETE);
        }
    }

    public void pause() {
        this.mOpHandler.removeMessages(OP_START);
        this.hasPendingPauseCommand = true;
        this.mOpHandler.sendEmptyMessage(OP_PAUSE);
    }

    private void pauseOtherMusicPlayer() {
        if (BaseApplication.getApplication() != null) {
            ((AudioManager) BaseApplication.getApplication().getSystemService(Context.AUDIO_SERVICE)).requestAudioFocus(null, 3, 1);
        }
    }

    public void preResolveInetAddressesFormHttpDns(int arg3) {
        try {
            if (!AppData.getInstance().getAbSettings().isVideoUseIpUrl()) {
                return;
            }

            if (SSMediaPlayerWrapper.mHttpDNSIpError >= 3) {
                return;
            }
            ArrayList<String> v0_1 = new ArrayList();
            v0_1.add("v4.pstatp.com");
            v0_1.add("v6.pstatp.com");
            v0_1.add("v7.pstatp.com");
            Iterator v1 = v0_1.iterator();
            while (v1.hasNext()) {
                this.resolveInetAddresses((String) v1.next());
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    public void prepare() {
        this.execAction(new Runnable() {

            public final void run() {
                if (mOpHandler != null) {
                    mOpHandler.sendEmptyMessage(OP_PREPARE_ASYNC);
                }
            }
        });
    }

    public void release() {
        this.mCurrentState = STAT_END;
        if (this.mMediaPlayer != null) {
            this.clearPendingActions();
            this.setQuietPlay(false);
            if (this.mOpHandler == null) {
                return;
            }

            try {
                this.removeTcpReadBytesMsg("release");
                this.mOpHandler.removeCallbacksAndMessages(null);
                this.mIsReleasing = true;
                this.mOpHandler.sendEmptyMessage(OP_RELEASE);
            } catch (Throwable throwable) {
            }
        }
    }

    private void removeTcpReadBytesMsg(String arg4) {
        if (this.mOpHandler != null) {
            this.mOpHandler.removeMessages(STAT_IDLE);
        }

        synchronized (readBytesLock) {
            if (this.readBytesBuilder != null) {
                SSMediaPlayerWrapper.onIjkLog(arg4 + " " + this.readBytesBuilder);
                this.readBytesBuilder = null;
            }
        }
    }

    public void requestCurPosition() {
        if (this.mOpHandler != null) {
            this.mOpHandler.obtainMessage(OP_REQUEST_CUR_POSITION).sendToTarget();
        }
    }

    public void requestDuration() {
        if (this.mOpHandler != null) {
            this.mOpHandler.obtainMessage(OP_REQUEST_DURATION).sendToTarget();
        }
    }

    public void reset() {
        this.execAction(new Runnable() {

            public final void run() {
                if (mOpHandler != null) {
                    mOpHandler.sendEmptyMessage(OP_RESET);
                }
            }
        });
    }

    public void resetVolume() {
        if (SSMediaPlayerWrapper.mIsVolumeChanged) {
            this.setVolume(this.mLastVolume, false);
            SSMediaPlayerWrapper.mIsVolumeChanged = false;
        }
    }

    private List<InetAddress> resolveInetAddresses(String inetAddress) {
        TextUtils.isEmpty(inetAddress);
        return null;
    }

    public void seekTo(final long msec) {
        if (this.mCurrentState == STAT_PAUSED
                || this.mCurrentState == STAT_STARTED
                || this.mCurrentState == STAT_PLAYBACK_COMPLETE) {
            this.execAction(new Runnable() {

                public final void run() {
                    if (mOpHandler != null) {
                        mOpHandler.obtainMessage(OP_SEEKTO, Long.valueOf(msec)).sendToTarget();
                    }
                }
            });
        }
    }

    public void setDataSource(final String path) {
        this.execAction(new Runnable() {
            public final void run() {
                initMediaPlayer();
                if (mOpHandler != null) {
                    mOpHandler.obtainMessage(OP_SET_DATASOURCE, path).sendToTarget();
                }
            }
        });
    }

    public void setDisplay(final SurfaceHolder surfaceHolder) {
        this.execAction(new Runnable() {

            public final void run() {
                initMediaPlayer();
                if (mOpHandler != null) {
                    mOpHandler.obtainMessage(OP_SET_DISPLAY, surfaceHolder).sendToTarget();
                }
            }
        });
    }

    public void setQuietPlay(boolean quiet) {
        if ((this.mMediaPlayer instanceof IjkMediaPlayer)) {
            if (quiet) {
                ((IjkMediaPlayer) this.mMediaPlayer).setPropertyLong(IJK_CLOSE_AUDIO_STREAM, 0L);
            } else {
                ((IjkMediaPlayer) this.mMediaPlayer).setPropertyLong(IJK_OPEN_AUDIO_STREAM, 0L);
            }
        }
    }

    public void setSurface(final Surface surface) {
        this.execAction(new Runnable() {
            public final void run() {
                initMediaPlayer();
                if (mOpHandler != null) {
                    mOpHandler.obtainMessage(OP_SET_SURFACE, surface).sendToTarget();
                }
            }
        });
    }

    public void setVolume(int volume, boolean arg5) {
        if (arg5) {
            int currentVolume = this.getCurrentVolume();
            if (currentVolume != volume) {
                SSMediaPlayerWrapper.mIsVolumeChanged = true;
                this.mLastVolume = currentVolume;
            }
        }
        ((AudioManager) BaseApplication.getApplication().getSystemService(Context.AUDIO_SERVICE)).setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0);
    }

    public void start(boolean prepareAsync, long position, boolean hasVoice) {
        this.hasPendingPauseCommand = false;
        if (hasVoice) {
            if (this.mMediaPlayer != null) {
                this.setQuietPlay(false);
            }
        } else {
            if (this.mMediaPlayer != null) {
                this.setQuietPlay(true);
            }
        }

        if (prepareAsync) {
            if (hasVoice) {
                this.pauseOtherMusicPlayer();
            }
            this.prepare();
            this.mFirstSeekToPosition = position;
        } else {
            this.execAction(new Runnable() {
                @Override
                public final void run() {
                    mOpHandler.sendEmptyMessageDelayed(OP_START, 50L);
                }
            });
        }
    }

    public void stop() {
        this.execAction(new Runnable() {
            public final void run() {
                if (mOpHandler != null) {
                    mOpHandler.sendEmptyMessage(OP_STOP);
                }
            }
        });
    }
}

